<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * Search for lists
 *
 * @package GeoContexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 828 $ / $LastChangedDate: 2011-02-27 10:30:28 +0100 (So, 27 Feb 2011) $ / $Author: armand.turpel $
 */

namespace Geocontexter\Controller;

use Zend\View\Model\ViewModel;
use Core\Controller\AbstractController;

class ListSearchController extends AbstractController
{
   public function init()
   {

        $this->initView( 'geocontexter/list-search/index.phtml' );

        $this->view->setTerminal(true);

        $this->view->list_result      = array();
        $this->view->error = array();

        $this->view->id_value = false;

    }

    /**
     *
     */
    public function indexAction()
    {
        $page = $this->request->getPost()->page;

        $itemCountPerPage = 5;

        // set offset for sql limit clause
        //
        if ((null === $page) || (1 == $page)) {
            $offset = 0;
            $page   = 1;
        } else {
            $offset = ($page - 1) * $itemCountPerPage;
        }

        $search_string = $this->request->getPost()->search_string;

        if (null === $search_string) {

            $search_string = $this->params()->fromRoute('item',false);

            if ($search_string === false) {
                $this->view->item_search_string = '';
                return $this->view;
            }
        }

        $this->view->item_search_string = $search_string;

        $ListSearch = $this->CoreModel('ListSearch');

        $params  = array('search'          => $search_string,
                         'default_display' => true,
                         'system_serial'   => true,
                         'limit'           => array($itemCountPerPage, $offset));

        $result = $ListSearch->run( $params );

        $totalNumRows = $ListSearch->totalNumRows();
        $this->view->num_rows = $totalNumRows;

        $this->view->result = $result;

        // build paginator result
        //
        $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\Null($totalNumRows));
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($itemCountPerPage);
        $paginator->setPageRange(10);
        \Zend\Paginator\Paginator::setDefaultScrollingStyle('Sliding');
        \Zend\View\Helper\PaginationControl::setDefaultViewPartial(
            'paginator-slide'
        );

        //this is paginator
        $this->view->paginator = $paginator;

        return $this->view;
    }
}

